package org.foreningsgatan.mercenaries.manual;


import java.util.List;

import org.foreningsgatan.mercenaries.Configuration;
import org.foreningsgatan.mercenaries.manual.shared.AbstractTerrainApplication;
import org.foreningsgatan.mercenaries.model.GroundDecal;
import org.foreningsgatan.mercenaries.model.LightSource;
import org.foreningsgatan.mercenaries.model.Vegetation;
import org.foreningsgatan.mercenaries.rendering.GroundDecalRenderer;
import org.foreningsgatan.mercenaries.rendering.LoggingUtils;
import org.foreningsgatan.mercenaries.rendering.Orientation;
import org.foreningsgatan.mercenaries.rendering.RenderUtils;
import org.foreningsgatan.mercenaries.rendering.VegetationRenderer;
import org.foreningsgatan.mercenaries.rendering.cache.VegetationCache;
import org.foreningsgatan.modular.common.performance.Timer;
import org.foreningsgatan.modular.lightsources.RGB;
import org.foreningsgatan.modular.resources.ResourceManager;
import org.foreningsgatan.modules.glcore.frames.SingleFrame;
import org.foreningsgatan.modules.glcore.graphics.Alignment;
import org.foreningsgatan.modules.glcore.graphics.BlendMode;
import org.foreningsgatan.modules.glcore.graphics.GLGraphics;
import org.foreningsgatan.modules.glcore.graphics.PackedTexture;
import org.foreningsgatan.modules.glcore.graphics.TextureManager;


/**
 * Copyright (c) 2008 Joakim Back
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * @author Joakim Back
 *
 */
public class ManualTestVegetation extends AbstractTerrainApplication {
	private List<Vegetation> mVegetation;
	
	private List<GroundDecal> mDecals;
	
	private SingleFrame mShadowFrame;
	
	private SingleFrame mLightFrame;

	// Renderers
	
	VegetationRenderer mVegetationRenderer;
	
	GroundDecalRenderer mGroundDecalRenderer;
	
	public ManualTestVegetation() {
		super("Vegetation Test", 800, 600, 60);
	}

	protected void init() throws Exception {
		super.init();
		
		mVegetation = mLevelLoader.loadVegetation();
		VegetationCache.getInstance().register(mVegetation);
		
		mDecals = mLevelLoader.loadDecals();
		
		mVegetationRenderer = new VegetationRenderer(mShadowFrame);
		mGroundDecalRenderer = new GroundDecalRenderer();
	}
	
	@Override
	protected void loadTextures(TextureManager tm) {
		super.loadTextures(tm);

		ResourceManager vManager = ResourceManager.getInstance();
		
		try {
			vManager.put("frames.vegetation.tree01", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.tree01")));
			vManager.put("frames.vegetation.tree02", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.tree02")));
			vManager.put("frames.vegetation.tree03", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.tree03")));
			vManager.put("frames.vegetation.tree04", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.tree04")));
			vManager.put("frames.vegetation.grass01", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.grass01")));
			vManager.put("frames.vegetation.bush01a", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.bush01a")));
			vManager.put("frames.vegetation.bush01b", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.bush01b")));
			vManager.put("frames.vegetation.bush01c", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.bush01c")));
			vManager.put("frames.vegetation.largegrass01", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.largegrass01")));
			vManager.put("frames.vegetation.sand01", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.sand01")));
			vManager.put("frames.vegetation.moss01", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.moss01")));
			vManager.put("frames.vegetation.leaves01", new SingleFrame((PackedTexture) vManager.get("textures.vegetation.leaves01")));
			
			mLightFrame = new SingleFrame((PackedTexture) vManager.get("textures.items.light"));
			mShadowFrame = new SingleFrame((PackedTexture) vManager.get("textures.items.shadow"));
		} catch (Exception e) {
			e.printStackTrace();

			System.exit(0);
		}
	}
	
	@Override
	protected void render(GLGraphics g) {
		super.render(g);
		
		/* Render Light Sources */
		
		for (LightSource vLightSource: mLightSources.getLightSources(Configuration.sMapSize, 0, 0)) {
			float x = RenderUtils.toScreenCoord(vLightSource.getX(), Orientation.HORIZONTAL, mScrollPosition);
			float y = RenderUtils.toScreenCoord(vLightSource.getY(), Orientation.VERTICAL, mScrollPosition);
			
			RGB vRGB = mLightMap.get((int) vLightSource.getX(), (int) vLightSource.getY());
			
			g.drawFrames(x, y - 16, 
					mLightFrame, Alignment.LOWER_MIDDLE, 1.0f, vRGB.getRGB(), BlendMode.ALPHA);
		}

		/* Render GroundDecals and Vegetation */

		Timer t = new Timer();
		
		mGroundDecalRenderer.update(mDecals, mLightMap);
		mVegetationRenderer.update(mLightSources, mLightMap);
		
		mGroundDecalRenderer.render(g, mClippingRect, mScrollPosition, mHasScrolled);
		mVegetationRenderer.render(g, mClippingRect, mScrollPosition, mHasScrolled);

		if (LoggingUtils.isActive) {
			mLog.debug("Vegetation took %s", t);
		}

		drawNavigation(g, 10, 10);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new ManualTestVegetation();
	}
}
